//
//  StateLabel.swift
//  777Proj
//
//  Created by 陈宇 on 2017/5/15.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit

fileprivate let padding: CGFloat = 5

@IBDesignable
class StateLabel: UILabel {
    
    enum State {
        case dark
        case bright
        case sale
        case fail
        
        func color() -> UIColor {
            switch self {
            case .dark:
                return UIColor(hex: 0xE85454)
            case .bright:
                return UIColor(hex: 0xfab939)
            case .sale:
                return UIColor(hex: 0x28aae9)
            case .fail:
                return UIColor(hex: 0xcccccc)
            }
        }
        
        func text() -> String {
            switch self {
            case .dark:
                return "暗标".localized()
            case .bright:
                return "明标".localized()
            case .sale:
                return "已售".localized()
            case .fail:
                return "失败".localized()
            }
        }
    }
    
    var state: State = .dark {
        didSet {
            text = state.text()
            setNeedsDisplay()
        }
    }
    
    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + padding * 2, height: size.height + padding)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configure()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        configure()
    }
    
    convenience init(state: State) {
        self.init()
        
        self.state = state
    }
    
    func configure() {
        textAlignment = .center
    }
    
    override func draw(_ rect: CGRect) {
        
        //// Background Rectangle Drawing
        let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: width, height: height), byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 5, height: 5))
        rectanglePath.close()
        state.color().setFill()
        rectanglePath.fill()
        
        super.draw(rect)
    }
    
}
